import React from 'react';

import {Button, Input, Select} from 'antd';
import ObjectList from '../../components/ObjectList';

import router from '../../../utils/routerDecorator';
import {get, post} from '../../../logic/api';

export default class Check extends React.Component {

  state = {
    status:"unchecked"
  }

  componentWillMount(){
    this.setState({
      status:this.props.record.status
    })
  }

  static after = {
    checked:{
      text:"取消审核成功",
      dest:"unchecked"
    },
    unchecked:{
      text:"审核成功",
      dest:"checked"
    }
  }

  componentWillReceiveProps(np){
    this.setState({
      status:np.record.status
    })
  }

  async docheck(){
    var id = this.props.record.id;
    var ret = await post('/admin/pub/check', {
      id:id,
      target:Check.after[this.state.status].dest
    });

    console.log(Check.after);

    if (ret.ok){
      alert(Check.after[this.state.status].text);
      this.setState({
        status:Check.after[this.state.status].dest
      });
    }else{
      alert("失败");
    }
  }

  render(){
    return <Button type = "primary" onClick = {()=>{this.docheck()}}>
      {this.state.status === "unchecked"?'审核':'取消审核'}
    </Button>
  }
}